package com.javasm.controller.MisMannageController;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.MisMannageEntity.ProdInfo;
import com.javasm.entity.MisMannageEntity.ReturnCode;
import com.javasm.entity.MisMannageEntity.ReturnEntity;
import com.javasm.service.MisMannageService.ReferralFundsService;
import com.javasm.service.MisMannageService.impl.ReferralFundsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@WebServlet("/referral/*")
public class ReferralFundsServlet extends BaseServlet {


    public void get(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String dateStr = req.getParameter("date");//月

        Integer date = 0;
        if (dateStr != null & !"".equals(dateStr)) {
            date = Integer.parseInt(dateStr);
        }

        LocalDate now = LocalDate.now();//当前日期

        System.out.println(now);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        //Date date = new Date(System.currentTimeMillis());
        LocalDate localDate = now.minusMonths(date);
        ZoneId zoneId = ZoneId.systemDefault();
        Instant instant = localDate.atStartOfDay().atZone(zoneId).toInstant();
        Date date1 = Date.from(instant);//date格式
        String format = formatter.format(date1);//string格式

        ReferralFundsService referralFundsService = new ReferralFundsServiceImpl();
        List<ProdInfo> prodInfos = referralFundsService.getReferr(format);

        ReturnEntity returnEntity = new ReturnEntity();
        if (prodInfos != null) {
            returnEntity.setRetCode(ReturnCode.QUERY_OK.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            returnEntity.setRetData(prodInfos);
        } else {
            returnEntity.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            returnEntity.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSON(returnEntity));
        System.out.println(JSON.toJSON(returnEntity));
        writer.flush();
        writer.close();

         /*select pi.prod_id,
                pi.prod_name,
                pi.series_id,
                ps.series_CHname,
                pi.second_type_id,
                pi.opening_time,
                pi.money_type,
                pi.count_growth_rate
        from tb_prod_info pi,tb_prod_series ps
        where pi.series_id= ps.series_id
        and opening_time>2022-05-22
        order by count_growth_rate DESC limit 0,6*/

    }
}
